// pages/placeOrder/placeorder.js
const app = getApp();
import { $wuxKeyBoard } from '../../../../components/wux'   //引入安全键盘
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lastTatalPrice: 0, //最后的总价
    add_res: null, //地址信息
    yuebox: true,
    isSetPass: null //是否设置过支付密码，0未设置，1已设置
  },
 
  onLoad: function (options) {
    this.setData({
      item_id: JSON.parse(options.info).item_id
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    wx.showLoading({ title: '加载中', mask: true });
    //发送下单请求
    wx.request({
      url: app.baseURL + 'Integral&a=or_detail',
      data: {
        item_id: this.data.item_id,
        user_id: app.globalData.userId,
        num: 1
      },
      success: res => {
        if (res.statusCode == 200) {
          const data = res.data;
          this.setData({
            info: data.integral_item_info,
            isSetPass: data.integral_item_info.is_pwd,
            add_res: data.add_res
          });
          wx.hideLoading();
        }
      }
    });
  },

  //点击提交按钮
  submitOrder(e){
    const isSetPass = this.data.isSetPass;

    if (isSetPass == 0) { 
      wx.showModal({
        title: '温馨提示',
        content: '你还未设置支付密码，去设置？',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/passRecoverySet/passRecoverySet',
            })
          }
        }
      }); return;}
    //显示密码支付弹窗
    let data, url;
    //如果没有添加收货地址提醒用户添加收货地址
    if (!this.data.add_res) { wx.showToast({ title: '请添加收货地址', icon: 'none' }); return; }
    //如果是从商品详情直接下单
    url = app.baseURL + 'Integral&a=convert';
    data = {
      num: this.data.info.total_num, //商品数量
      address_id: this.data.add_res.id, //地址id
      item_id: this.data.info.id, //商品id
      user_id: app.globalData.userId //用户id
    }
    //如果用户有备注，则提交备注字段
    e.detail.value.message && (data.comment = e.detail.value.message); 
    wx.showLoading({ title: '请稍后', mask: true });
    if (this.data.info.total_price > 0){
      //发送下单请求
      wx.request({
        url: url,
        data: data,
        success: (res) => {
          if (res.data.status == 1 && res.statusCode == 200) {
            wx.hideLoading();
            let result = JSON.stringify(res.data.order_info);
            //跳转支付页面
            wx.redirectTo({
              url: '/pages/orderPay/orderPay?result=' + result + '&integral=1'
            })
          }
          if (res.data.status == 2){
            wx.showToast({
              title: res.data.info,
              icon: 'none'
            });
          }
        }
      })
    }else{
      //显示密码支付弹窗
      this.enterPay();
      wx.hideLoading();
    }
    
  },

  //余额支付弹窗-确认支付
  enterPay() {
    const thta = this;
    $wuxKeyBoard.show({
      className: 'className',
      titleText: '安全键盘',
      cancelText: '取消',
      inputText: '输入数字密码',
      showCancel: true,
      disorder: false,
      callback(value) {
        const info = thta.data;
        const data = {
          user_id: app.globalData.userId, //用户id
          item_id: info.info.id, //商品id
          num: 1, //商品数量
          address_id: info.add_res.id, //地址id
          pwd: value //支付密码
        }
        wx.showLoading({ title: '加载中' });
        wx.request({
          url: app.baseURL + 'Integral&a=fast_pay',
          data: data,
          success: (res) => {
            if (res.data.status == 1) {
              wx.showLoading({ title: '请稍后...' });
              wx.reLaunch({
                url: '/pages/commentResult/commentResult?title=兑换成功',
              })
            } else if (res.data.status != 1) {
              wx.showToast({ title: res.data.info, icon: 'none' });
            }

          },
          fail: (res) => {
            wx.showToast({ title: '网络错误', icon: 'none' })
            wx.hideLoading();
          }
        })
      },
    });  
  },

  //关闭余额支付弹窗
  closeYueBox() {
    this.setData({
      yuebox: true,
      password: ''
    })
  },

  //前往添加收货地址
  toAddAddress() {
    wx.navigateTo({
      url: '/pages/addressAdd/addressAdd?formWeb=order',
    })
  },

  //去选择收货地址
  selectAddress() {
    wx.navigateTo({
      url: '/pages/address/address?select=true',
    })
  },

})